local blacklistbutton = CreateFrame("button", "SafeAuctionBlackListButton", AuctionFrame, "UIPanelButtonTemplate")
blacklistbutton:SetSize(80, 22)
blacklistbutton:SetText("Blacklist")
blacklistbutton:SetPoint("RIGHT", "BrowseSearchButton","LEFT", -45, 0)

SafeAuctionBlackList = SafeAuctionBlackList or {}

local floor, ceil, mod = math.floor, math.ceil, math.fmod
do
	local i = 1
	while _G["BrowseButton"..i] do
		local t = _G["BrowseButton"..i.."BuyoutFrame"]:CreateTexture(nil, "artwork")
		t:SetTexture(1, 1, 1, 0.4)
		t:SetGradient("horizontal", 0, 0, 0, 1, 0, 0)
		t:SetPoint("TOPLEFT", -2, 17)
		t:SetPoint("BOTTOMRIGHT", -14, -2)
		local btn = _G["BrowseButton"..i]
		_G["BrowseButton"..i.."BuyoutFrame"]:SetScale(0.85)
		_G["BrowseButton"..i.."MoneyFrame"]:SetScale(0.85)
		btn._buyoutText =  _G["BrowseButton"..i.."BuyoutFrameText"]:GetRegions()
		btn._buyoutText:SetJustifyV("bottom")
		btn._buyoutText:SetHeight(40)
		btn._buyoutText:ClearAllPoints()
		btn._buyoutText:SetPoint("TOPLEFT", 0, 28)
		btn._iconTexture = _G["BrowseButton"..i.."ItemIconTexture"]
		btn._highlight = _G["BrowseButton"..i.."Highlight"]
		btn._closingTime = _G["BrowseButton"..i.."ClosingTime"]
		btn._warning = t
		i = i + 1
	end
end
local function GetMoneyString(x)
	local g, s, c = floor(x / 10000), floor(mod(x, 10000)/100), mod(x, 100)
	local t = ""
	if g > 0 then t = "|CFFFFD700"..g.."|r"end
	if s > 0 then t = t.."|CFFCCCCCC.|CFFC7C7CF"..s.."|r" end
	if c > 0 then
		if s == 0 and g > 0 then
			t = t.."|CFFCCCCCC.|CFFC7C7CF00"
		end
		t = t.."|CFFCCCCCC.|CFF9A4833"..c.."|r"
	end
	return t
end
local userName = UnitName("player")
local fmt = "%s (%s)"
hooksecurefunc("AuctionFrameBrowse_Update", function()
	for i = 1, NUM_BROWSE_TO_DISPLAY do
		local button = _G["BrowseButton"..i]
		button._warning:Hide()
		button._buyoutText:SetTextColor(1, 0.82, 0)
		button._iconTexture:SetVertexColor(1, 1, 1, 1)
		button._highlight:SetVertexColor(1, 1, 1)
		button._buyoutText:SetText("즉시 구입:")
		button:EnableMouse(true)
		button._closingTime:EnableMouse(true)
		button:SetAlpha(1)
		local name, texture, count, quality, canUse, level, levelColHeader, minBid, minIncrement, buyoutPrice, bidAmount, highBidder, owner =  GetAuctionItemInfo("list", FauxScrollFrame_GetOffset(BrowseScrollFrame) + i);
		if SafeAuctionBlackList[owner] then
			button:EnableMouse(false)
			button:SetAlpha(0.2)
			button._closingTime:EnableMouse(false)
		else
			if buyoutPrice > 0 then
				local curr = (bidAmount == 0 and minBid or bidAmount) * 3
				if count > 1 then
					_G["BrowseButton"..i.."Name"]:SetFormattedText(fmt, name, GetMoneyString(ceil(buyoutPrice / count)))
				end

				if owner == userName then
					button._iconTexture:SetVertexColor(0, 1, 0)
					button._highlight:SetVertexColor(0, 1, 0)
					button._buyoutText:SetText("내 물건:")
					button._buyoutText:SetTextColor(0, 1, 0)
				elseif buyoutPrice > 50000 and buyoutPrice > curr then
					button._iconTexture:SetVertexColor(1, 0.3, 0, 0.3)
					button._highlight:SetVertexColor(1, 0, 0)
					button._buyoutText:SetText("차액(골):\n"..floor((buyoutPrice-minBid)/10000))
					button._buyoutText:SetTextColor(1, 0.82, 0)
					button._warning:Show()
				end
			end
		end
	end
end)

hooksecurefunc("BrowseButton_OnClick", function()
	HideDropDownMenu(1)
end)
local function addblacklist(self, arg1)
	SafeAuctionBlackList[arg1] = true
	AuctionFrameBrowse_Update()
end
local function removeblacklist(self, arg1)
	SafeAuctionBlackList[arg1] = nil
	AuctionFrameBrowse_Update()
end
local sorter = {}
local dropdown = CreateFrame("frame", "$parentDropdownMenu", blacklistbutton, "UIDropDownMenuTemplate")
UIDropDownMenu_Initialize(dropdown, function(self, level)
	if not level then return end
	if level == 1 then
		local owner =  select(13, GetAuctionItemInfo("list", GetSelectedAuctionItem("list")))
		if owner then
			local info = UIDropDownMenu_CreateInfo()
			info.func = addblacklist
			info.arg1 = owner
			info.notCheckable = true
			info.fontObject = "ChatFontNormal"
			info.text = "|cff00ff00< "..owner.." >|r 등록"
			UIDropDownMenu_AddButton(info)
		end

		info = UIDropDownMenu_CreateInfo()
		info.notCheckable = true
		info.text = "블랙리스트 삭제"
		info.hasArrow = true

		UIDropDownMenu_AddButton(info)
	else
		wipe(sorter)
		for name in pairs(SafeAuctionBlackList) do	
			table.insert(sorter, name)
		end
		table.sort(sorter)
		info = UIDropDownMenu_CreateInfo()
		info.notCheckable = true
		info.func = removeblacklist
		if #sorter == 0 then
			info.isTitle = true
			info.notClickable = true
			info.text = "리스트 없음"
			info.justufyH = "CENTER"
			UIDropDownMenu_AddButton(info, level)
		end
		for _,name in ipairs(sorter) do
			info.arg1 = name
			info.text = name
			UIDropDownMenu_AddButton(info, level)
		end
	end
end, "MENU")
blacklistbutton:SetScript("OnClick", function(self)
	ToggleDropDownMenu(1, nil, dropdown, blacklistbutton, 0, -10)
	UIDropDownMenu_Refresh(dropdown)
end)